AdminDialogPageBinding.prototype = new DialogPageBinding;
AdminDialogPageBinding.prototype.constructor = AdminDialogPageBinding;
AdminDialogPageBinding.superclass = DialogPageBinding.prototype;

/*

	HOW TO USE:
	
	var handler = {
			handleDialogResponse : function ( response, result ) {
				if ( response == Dialog.RESPONSE_ACCEPT ) {
					getQueryTreeBinding ().buildFromServer ( result.get ( 0 ));
				}
			}
		} 
		var arg = {
			label 				: "Select Image",
			key 				: "ReadOnlyXmlProviderElementProvider",
			selectionProperty 	: "ElementType",
			selectionValue		: "image/jpg image/gif image/png",
			selectionResult		: "ElementId"
			//optional , width : 400
			//optional , height : 400
		}
		Dialog.invokeModal ( Dialog.URL_TREESELECTOR, handler, arg );
*/

/**
 * @class
 */
function AdminDialogPageBinding () {

	/**
	 * @type {SystemLogger}
	 */
	this.logger = SystemLogger.getLogger ( "AdminDialogPageBinding" );
	
	/**
	 * @type {string}
	 *
	this._key = null;
	*/
	
}

/**
 * Identifies binding.
 */
AdminDialogPageBinding.prototype.toString = function () {

	return "[AdminDialogPageBinding]";
}

/** 
 * Fetch properties from page argument.
 * @overloads {PageBinding#setPageArgument}
 * @param {object} arg
 */
AdminDialogPageBinding.prototype.setPageArgument = function (arg) {

    AdminDialogPageBinding.superclass.setPageArgument.call(this, arg);

    if (arg.label) {
        this.label = arg.label;
    }
    if (arg.image) {
        this.image = arg.image;
    }
    if (arg.width) {
        this.width = arg.width;
    }
    if (arg.height) {
        this.height = arg.height;
    }

    if (arg.data) {
        if ($('#IsPostBack').val() != 'True') {
            var elemsToTriggerChange = [];

            $('input, select, textarea').each(function () {
                var name = this.name;

                if (name != "" && typeof (name) != "undefined") {
                    name = name.replace(/^(.*)\$ddlLookupData$/g, "$1");
                    name = name.replace(/^([\w\$]*\$)*?(\w+)$/g, "$2");

                    if (arg.data[name]) {
                        var isChanged = false;

                        if ($(this).is(':checkbox')) {
                            this.checked = arg.data[name].toString().toLowerCase() == "true";
                        } else {
                            $(this).val(arg.data[name]);
                        }

                        if (typeof ($(this).attr("onchange")) != "undefined") {
                            if (!($(this).val() == "" ||
                                  ($(this).is('select') && $(this).val() == "NULL"))) {
                                elemsToTriggerChange.push(this);
                            }
                        }
                    }
                }
            });

            if (elemsToTriggerChange.length > 0) {
                window.setTimeout(function () {
                    $(elemsToTriggerChange[0]).trigger('change');
                }, 500);
            }
        }
    }

    if (typeof (arg.isSupportLanguageEn) != "undefined" || typeof (arg.isSupportLanguageFr) != "undefined") {
        $('.lang-en').css('display', arg.isSupportLanguageEn ? '' : 'none');
        $('.lang-fr').css('display', arg.isSupportLanguageFr ? '' : 'none');

        enableValidators($('.lang-en'), arg.isSupportLanguageEn);
        enableValidators($('.lang-fr'), arg.isSupportLanguageFr);
    }
}

AdminDialogPageBinding.prototype.validateAllDataBindings = function (_d5f) {
    
    var isValid = Page_ClientValidate("");
    Page_BlockSubmit = false;
    return isValid;

}

/**
 * @overloads {DialogPageBinding#onDialogAccept}
*/
AdminDialogPageBinding.prototype.onDialogAccept = function () {
    /*if (!Page_ClientValidate("")) {
        return false;
    }*/

    var inputValues = new Array();
    $('input, select, textarea').each(function () {
        var name = this.name;

        if (name != "" && typeof (name) != "undefined") {
            //fix .net bug for radio with groupname
            if ($(this).is(':radio'))
              name = this.value;
              
            name = name.replace(/^(.*)\$ddlLookupData$/g, "$1");
            name = name.replace(/^([\w\$]*\$)*?(\w+)$/g, "$2");

            if ($(this).is(':checkbox')) {
                inputValues[name] = this.checked;
            }
            else if ($(this).is(':radio')) {
                inputValues[name] = this.checked;
            }
            else {
                inputValues[name] = $(this).val();
            }

            if ($(this).is('select')) {
                inputValues[name + ".text"] = $(this).find('option:selected').text();
            }
        }
    });

    this.result = inputValues;

    AdminDialogPageBinding.superclass.onDialogAccept.call(this);

}